package com.hrt.datasender.datadao.model;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import com.btcode.exception.BusinessException;
import com.btcode.orm.model.IDomainField;
import com.btcode.orm.model.IField;
import com.btcode.orm.model.IModelAddition;
import com.btcode.orm.relation.ex.IRelation;
import com.hrt.datasender.datadao.service.BcDeviceService;

public class BcDevicesModelAddition implements IModelAddition<BcDevicesModel>, Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    @Override
    public void generateDefaultValue(BcDevicesModel model) {

        if (model.getCreatetime() == null) {
            model.setCreatetime(new Date());
        }

        if (model.getId() == null || model.getId().trim().equals("")) {
            model.setId(UUID.randomUUID().toString());
        }

        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd_HH:mm:ss");

        model.setCreatetimeText(sf.format(model.getCreatetime()));

    }

    @Override
    public void initField(List<IField> fields) {
        // TODO Auto-generated method stub

    }

    @Override
    public void initRelations(List<IRelation> relations) {
        // TODO Auto-generated method stub

    }

    @Override
    public void initDomainField(List<IDomainField> domainFields) {
        // TODO Auto-generated method stub

    }

    @Override
    public void beforeCreate(BcDevicesModel model) {

        List<BcDevicesModel> list = BcDeviceService.getInstance().getDevices();
        for (int i = 0; i < list.size(); i++) {
            if (model.getDeviceName().equals(list.get(i).getDeviceName())) {
                throw new BusinessException("设备名称不能重复");
            }
        }

        if (model.getComNum() == null || model.getComNum().equals("")) {

            throw new BusinessException("请选择串口号");
        }
        else if (model.getDeviceName() == null || model.getDeviceName().equals("")) {

            throw new BusinessException("设备名称不能为空");
        } 
        else if (model.getProtocolId() == null || model.getProtocolId().equals("")) {

            throw new BusinessException("请选择协议");
        }

    }

    @Override
    public void afterCreate(BcDevicesModel model) {
        // TODO Auto-generated method stub

    }

    @Override
    public void beforeUpdate(BcDevicesModel model) {
        List<BcDevicesModel> list = BcDeviceService.getInstance().getDevices();

        for (int i = 0; i < list.size(); i++) {
            if (!(model.getId()).equals(list.get(i).getId())) {

                if (model.getDeviceName().equals(list.get(i).getDeviceName())) {
                    throw new BusinessException("设备名不能重复");
                }
            }
        }
    }

    @Override
    public void afterUpdate(BcDevicesModel model) {
        // TODO Auto-generated method stub

    }

    @Override
    public void beforeDelete(BcDevicesModel model) {
        // TODO Auto-generated method stub

    }

    @Override
    public void afterDelete(BcDevicesModel model) {
        // TODO Auto-generated method stub

    }

}
